/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年5月25日
 */
package com.massyframework.beanskin.framework;

/**
 * 运行框架事件适配器，对{@link Framework}各种事件进行封装。
 * @author huangkh
 *
 */
public class FrameworkEventAdapter implements FrameworkListener {

	/**
	 * 
	 */
	public FrameworkEventAdapter() {
	}

	@Override
	public final void onEvent(FrameworkEvent event) {
		FrameworkInitializingEvent initializingEvent =
				this.asFrameworkEvent(event, FrameworkInitializingEvent.class);
		if (initializingEvent != null) {
			this.onEvent(initializingEvent);
			return;
		}
		
		FrameworkInitializedEvent initializedEvent =
				this.asFrameworkEvent(event, FrameworkInitializedEvent.class);
		if (initializedEvent != null) {
			this.onEvent(initializedEvent);
			return;
		}
		
		FrameworkStartingEvent startingEvent =
				this.asFrameworkEvent(event, FrameworkStartingEvent.class);
		if (startingEvent != null) {
			this.onEvent(startingEvent);
			return;
		}
		
		FrameworkStartedEvent startedEvent =
				this.asFrameworkEvent(event, FrameworkStartedEvent.class);
		if (startedEvent != null) {
			this.onEvent(startedEvent);
			return;
		}
				
		FrameworkStoppingEvent stoppingEvent =
				this.asFrameworkEvent(event, FrameworkStoppingEvent.class);
		if (stoppingEvent != null) {
			this.onEvent(stoppingEvent);
			return;
		}
		
		FrameworkStoppedEvent stoppedEvent =
				this.asFrameworkEvent(event, FrameworkStoppedEvent.class);
		if (stoppedEvent != null) {
			this.onEvent(stoppedEvent);
			return;
		}
		
		FrameworkDestroyingEvent destoryEvent =
				this.asFrameworkEvent(event, FrameworkDestroyingEvent.class);
		if (destoryEvent != null) {
			this.onEvent(destoryEvent);
			return;
		}
	}
	
	/**
	 * 开始初始化事件处理
	 * @param event {@link FrameworkInitializingEvent}
	 */
	public void onEvent(FrameworkInitializingEvent event) {
		
	}
	
	/**
	 * 初始化完成事件处理
	 * @param event {@link FrameworkInitializingEvent}
	 */
	public void onEvent(FrameworkInitializedEvent event) {
		
	}
	
	/**
	 * 开始启动事件处理
	 * @param event {@link FrameworkStartingEvent}
	 */
	public void onEvent(FrameworkStartingEvent event) {
		
	}
	
	/**
	 * 结束启动事件处理
	 * @param event {@link FrameworkStartedEvent}
	 */
	public void onEvent(FrameworkStartedEvent event) {
		
	}
	
	
	/**
	 * 启动停止事件处理
	 * @param event {@link FrameworkStoppingEvent}
	 */
	protected void onEvent(FrameworkStoppingEvent event) {
		
	}
	
	/**
	 * 完成停止事件处理
	 * @param event {@link FrameworkStoppedEvent}
	 */
	public void onEvent(FrameworkStoppedEvent event) {
		
	}
	
	/**
	 * 正在销毁事件处理
	 * @param event {@link FrameworkDestroyingEvent}
	 */
	public void onEvent(FrameworkDestroyingEvent event) {
		
	}
	
	/**
	 * 将{@code event}转换为指定类型的事件
	 * @param <T>
	 * @param event {@link FrameworkEvent}
	 * @param eventType {@link Class},其他类型的Framework事件
	 * @return {@link T},如果不支持，可以返回null.
	 */
	protected <T> T asFrameworkEvent(FrameworkEvent event, Class<T> eventType) {
		if (eventType.isAssignableFrom(event.getClass())) {
			return eventType.cast(event);
		}
		return null;
	}

}
